#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
char path[30] = "/run/media/sda3";
//获取时间
void  gettime(char _time[])
{
     time_t time3;
     struct tm *p;
     time3 = time(NULL);
    
    p = localtime(&time3);
   
    // printf("%d-%d-%d%d:%d:%d\n", (1900 + p->tm_year), ( 1 + p->tm_mon), p->tm_mday,
    //                             p->tm_hour, p->tm_min, p->tm_sec);

    sprintf(_time,"%04d-%02d-%02d %02d-%02d-%02d", (1900 + p->tm_year), ( 1 + p->tm_mon), p->tm_mday,
                                p->tm_hour, p->tm_min, p->tm_sec);

}

//创建文件
void file_create(char filename[])
{
    
    int fd;
    char _filename[50] = "";
    strcat(path,"/");
    strcpy(_filename, path);
    strcat(filename,".txt");
    strcat(_filename,filename);
     printf("%s\n",_filename);
    fd = open(_filename,O_RDWR|O_CREAT);
    if(fd<0)
    {
        printf("open%serror:%s",_filename,strerror(errno));
    }    
    close(fd);
    
 }

// 遍历文件夹
void ergodic_file(char file_list[][50])
{
    struct dirent *dir;
    DIR *dirp;
    int i = 0; 
    int ret = 0;
    /* 打开目录 */
    dirp = opendir(path);
    if (NULL == dirp) 
    {
        perror("opendir error");
    }
    /* 循环读取目录流中的所有目录条目,readdir出错会设置errno */
    errno = 0;
    while (NULL != (dir = readdir(dirp)))
    {
        //printf("%s %ld\n", dir->d_name, dir->d_ino);
        if(strncmp(dir->d_name, "20", 2)==0)
        {
            strcpy(file_list[i],dir->d_name ); //存储到数组
            i++;
        }
    }
    
    if (0 != errno) 
    {
        perror("readdir error");
        ret = -1;
        goto err;
    }
     else
        printf("End of directory!\n");
         goto err;
    err:
        closedir(dirp);
    //数组排序,由小到大
     char middle[50] ;
    for(int times=0;times<i;times++)
    {
            
            for(int j=0;j<i-1-times;j++)
            {
                for(int k=0;k<40;k++ )
                {
                    if(file_list[j][k]>file_list[j+1][k])
                    {
                        for(int c=0;c<strlen(file_list[0]);c++)
                        {
                            middle[c] = file_list[j][c];
                            file_list[j][c] = file_list[j+1][c]; 
                            file_list[j+1][c] = middle[c];
                        }  
                        break;
                    }
                     else if(file_list[j][k]<file_list[j+1][k])
                    {
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
   }

}
int main(int argc, char *argv[])
{
if(argv[1]!=NULL)
{
  strcpy(path,argv[1]);
}
char filename[50] ;
int count = 10;


while(count--)
{
   
    gettime(filename);
    file_create(filename);
    memset(filename, 0, sizeof(filename));
    sleep(1);
}
printf("---------------------readfile------------------------\n");
char file_list[1000][50];
ergodic_file( file_list);
for(int i =0;i<1000;i++)
{
    if(file_list[i][0]!=0)
    {
        printf("%s\n",file_list[i]);
    }
    else
    {
        printf("filenum=%d\n",i);
        break;
    }
    
}
    return 0;
    
}